package com.yuma.tingshu.order.template;

import com.yuma.tingshu.album.client.AlbumInfoFeignClient;
import com.yuma.tingshu.common.constant.SystemConstant;
import com.yuma.tingshu.common.execption.YumaException;
import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.common.result.ResultCodeEnum;
import com.yuma.tingshu.common.util.AuthContextHolder;
import com.yuma.tingshu.model.album.AlbumInfo;
import com.yuma.tingshu.model.user.UserInfo;
import com.yuma.tingshu.order.template.anno.TradBean;
import com.yuma.tingshu.user.client.UserInfoFeignClient;
import com.yuma.tingshu.vo.order.OrderDerateVo;
import com.yuma.tingshu.vo.order.OrderDetailVo;
import com.yuma.tingshu.vo.order.OrderInfoVo;
import com.yuma.tingshu.vo.order.TradeVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@TradBean(value = SystemConstant.ORDER_ITEM_TYPE_ALBUM)
public class AlbumTrade extends TradeTemplate{

    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Resource
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Override
    public void buildOrderInfoVo(OrderInfoVo orderInfoVo, TradeVo tradeVo) {
        //----------4.2 专辑订单
        // 1.获取登录用户id
        Long userId = AuthContextHolder.getUserId();
        // 2.根据用户id和专辑id查询user_paid_album表中查询是否购买过
        Result<Boolean> paidAlbumResult = userInfoFeignClient.isPaidAlbum(tradeVo.getItemId());
        Assert.notNull(paidAlbumResult, "专辑详情的订单页面，根据用户id和专辑id查询用户是否购买专辑失败");
        Boolean isPaid = paidAlbumResult.getData();
        // 2.1.如果没购买过
        if (!isPaid) {
            // 3.1 根据userId查询userinfo表，是否是没过期的vip用户
            Result<UserInfo> userInfoResult = userInfoFeignClient.getUserById(userId);
            Assert.notNull(userInfoResult, "专辑详情的订单页面，根据用户id查询用户信息失败");
            UserInfo userInfo = userInfoResult.getData();
            Assert.notNull(userInfo, "专辑详情的订单页面，根据用户id查询用户信息失败");
            // 3.2 根据itemId(专辑id)查询专辑的信息
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(tradeVo.getItemId());
            Assert.notNull(albumInfoResult, "专辑详情的订单页面，根据专辑id查询专辑信息失败");
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "专辑详情的订单页面，根据专辑id查询专辑信息失败");
            // 设置原价
            orderInfoVo.setOriginalAmount(albumInfo.getPrice().setScale(2));
            // 3.3 如果是vip用户，orderPrice是vip_discount
            if (userInfo.getIsVip().intValue() == 1 && userInfo.getVipExpireTime().after(new Date())) {
                if(albumInfo.getVipDiscount().compareTo(BigDecimal.ZERO) > 0) {
                    // 订单价格 ：原价 * vip_discount/10
                    orderInfoVo.setOrderAmount((albumInfo.getVipDiscount().divide(BigDecimal.valueOf(10))).multiply(albumInfo.getPrice()).setScale(2));
                    // 原价 — 订单价格
                    orderInfoVo.setDerateAmount(albumInfo.getPrice().subtract(orderInfoVo.getOrderAmount()).setScale(2));
                }else {
                    orderInfoVo.setOrderAmount(albumInfo.getPrice().setScale(2));// 订单价格同原价
                    orderInfoVo.setDerateAmount(BigDecimal.ZERO.setScale(2));
                }
            }else {
                if(albumInfo.getDiscount().compareTo(BigDecimal.ZERO) > 0) {
                    // 3.3 否则不是vip用户，deratePrice是discount
                    orderInfoVo.setOrderAmount((albumInfo.getDiscount().divide(BigDecimal.valueOf(10))).multiply(albumInfo.getPrice()).setScale(2));
                    // 原价 — 订单价格
                    orderInfoVo.setDerateAmount(albumInfo.getPrice().subtract(orderInfoVo.getOrderAmount()).setScale(2));
                }else {
                    orderInfoVo.setOrderAmount(albumInfo.getPrice().setScale(2));// 订单价格同原价
                    orderInfoVo.setDerateAmount(BigDecimal.ZERO.setScale(2));
                }
            }
            // 4.封装订单详情集合
            orderInfoVo.setOrderDetailVoList(List.of(
                    new OrderDetailVo()
                            .setItemId(tradeVo.getItemId())
                            .setItemName(albumInfo.getAlbumTitle())
                            .setItemPrice(albumInfo.getPrice())
                            .setItemUrl(albumInfo.getCoverUrl())
            ));
            // 5.封装订单减免集合
            orderInfoVo.setOrderDerateVoList(List.of(
                    new OrderDerateVo()
                            .setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT)
                            .setDerateAmount(orderInfoVo.getDerateAmount())
                            .setRemarks("专辑:" + albumInfo.getAlbumTitle())
            ));
        }else {
            // 2.2 购买过，抛出不可重复购买异常
            throw new YumaException(ResultCodeEnum.ORDER_SUBMIT_REPEAT);
        }
    }
}
